package com.ruoyi.system.util;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlRegexReplace {





    public static String  regexReplace(String string, Map<String,String> map){
        Pattern compile = Pattern.compile("\\$\\{(.+?)\\}");
        Matcher matcher = compile.matcher(string);
        StringBuffer stringBuffer = new StringBuffer();
        while (matcher.find()){
            String group = matcher.group(1);
            matcher.appendReplacement(stringBuffer,map.getOrDefault(group,""));
        }
        matcher.appendTail(stringBuffer);
        return stringBuffer.toString();

    }


    public static String regexReplace2(String input, Map<String, String> replacements) {

        String result =  input;
        for (Map.Entry<String,String> entry : replacements.entrySet()){
            String key = entry.getKey();
            String value = entry.getValue();
            result = replacePlaceHolderValue(result,key,value);
        }

    return  result;

    }

    private static String replacePlaceHolderValue(String result, String key, String value) {
        String regex = "\\$\\{"+Pattern.quote(key)+"}";
        return result.replaceAll(regex,value);
    }
}
